<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\Responder;
use StudyBuddy\WebPage;
use StudyBuddy\Paginator;
use StudyBuddy\Template\Urhere;
use StudyBuddy\Request;

/**
 * Controller for rendering
 * the "Members" page
 *
 * If request is by Ajax, it returns only the content
 * of the main area, paginated, sorted and with pagination
 * links if necessary
 *
 */
class Editbadges extends WebPage {

    protected $layoutID = 1;
    protected $qtab = 'questions';
    protected $oCursor;
    protected $Html;
    protected $bid;
    protected $submitedit;
    protected $permission = 'delete_answer';

    protected function main() {

        $this->init()
                ->getCursor()
                ->renderHtml();

        $this->setTitle()
                ->makeTopTabs()
                ->setUsers();
    }

    protected function setTitle() {
        $title = 'Edit Badges';
        $this->aPageVars['title'] = $title;
        $this->aPageVars['qheader'] = '<h1>' . $title . '</h1>';

        return $this;
    }

    protected function init() {

        $this->submitedit = $this->oRegistry->Request->get('submitedit', 's', '');

        $this->getUser();

        return $this;
    }

    protected function getUser() {

        $uid = $this->oRequest->get('uid', 'i', null);
        if ($uid && ($uid !== $this->oRegistry->Viewer->getUid())) {
            $this->checkAccessPermission($permission);
            $this->oUser = \StudyBuddy\User::factory($this->oRegistry)->by_id($uid);
        } else {
            $this->oUser = $this->oRegistry->Viewer;
        }

        return $this;
    }

    protected function makeTopTabs() {
        $guest = null;
        if($this->oRegistry->Viewer->isGuest()) {
            $guest['link_ask'] = '#'; // 13
            $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
        }
        d('cp');
        $tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', $this->qtab, $guest);
        $this->aPageVars['topTabs'] = $tabs;

        return $this;
    }

    protected function getCursor() {

        if (empty($this->submitedit)) {
            $this->bid = $this->oRegistry->Request->get('bid', 'i', null);
            $where = array('id' => $this->bid);
            $this->oCursor = $this->oRegistry->Mongo->BADGES->findOne($where);
        } else {
            $arr = array(
                '$set' => array(
                    'b_name' => $this->oRegistry->Request->get('b_name', 's', ''),
                    'b_desc' => $this->oRegistry->Request->get('b_desc', 's', ''),
                    'cond_type' => $this->oRegistry->Request->get('cond_type', 's', ''),
                    'cond' => $this->oRegistry->Request->get('cond', 's', ''),
                    'type' => $this->oRegistry->Request->get('type', 's', '')
                )
            );
            $this->oRegistry->Mongo->BADGES->update(array('id' => $this->oRegistry->Request->get('bid', 'i', null)), $arr);
        }
        return $this;
    }

    protected function buildCondType($current = '') {
        $s = '';
        $subject = array(
            'a_answer_voted_over' => 'When have a answer with over xxx voted',
            'a_question_voted_over' => 'When have a question with over xxx voted',
            'a_question_view_over' => 'When have a question with over xxx viewed',
            'question_total' => 'When has total questions over xxx',
            'answer_total' => 'When has total answers over xxx',
            'vote_total' => 'When has total vote over xxx',
            'comment_total' => 'When has total comments over xxx',
            'retag' => 'When has total retag questions over xxx',
            'question_edited_total' => 'When has total questions was edited over xxx',
            'report_abuse_total' => 'When has total questions was reported abuse over xxx',
            'answer_voted_total' => 'When has total voted for answers over xxx',
            'question_voted_total' => 'When has total voted for questions over xxx',
        );

        $tpl = '<option value="%1$s"%2$s>%3$s</option>';
        foreach ($subject as $key => $val) {
            $selected = ($key == $current) ? ' selected="selected"' : '';
            $s .= vsprintf($tpl, array($key, $selected, $val));
        }

        return $s;
    }
    
    protected function buildType($current =''){
        $s = '';
        $subject = array(
            'gold' => 'gold',
            'silver' => 'silver',
            'bronze' => 'bronze'
        );

        $tpl = '<option value="%1$s"%2$s>%3$s</option>';
        foreach ($subject as $key => $val) {
            $selected = ($key == $current) ? ' selected="selected"' : '';
            $s .= vsprintf($tpl, array($key, $selected, $val));
        }

        return $s;
        
    }

    protected function renderHtml() {

        if (empty($this->submitedit)) {

            $this->Html = '<div class="ctl-ct badgeswrap"><h3>Edit Badges</h3>';
            $this->Html .= '<div style="padding:50px;">
                            <form action="/index.php">
                            <input type="hidden" value="editbadges" name="a">
                            <input type="hidden" value="1" name="submitedit">
                            <input type="hidden" value="' . $this->oCursor['id'] . '" name="bid">
                            <table>';

            $this->Html .= '<tr height="35px">
                                    <td class="check-cell"><span>ID:</span></td>
                                    <td class="check-cell"><span>' . $this->oCursor['id'] . '</span></td>
                            </tr>
                            <tr height="35px">	
                                    <td class="check-cell"><span>Name: </span></td>
                                    <td class="check-cell"><span><input type="text" name="b_name" value="' . $this->oCursor['b_name'] . '"/></span></td>
                            </tr>
                            <tr height="35px">	
                                    <td class="check-cell"><span>Condition Type: </span></td>
                                    <td class="check-cell"><select name="cond_type" >' . $this->buildCondType($this->oCursor['cond_type']) . '</select></td>
                            </tr>
                            <tr height="35px">	
                                    <td class="check-cell"><span>Condition: </span></td>
                                    <td class="check-cell"><span><input type="text" name="cond" value="' . $this->oCursor['cond'] . '"/></span></td>
                            </tr>
                            <tr height="35px">
                                    <td><span>Description:</span></td>
                                    <td><input type="text" name="b_desc" value="' . $this->oCursor['b_desc'] . '"/></td>
                            </tr>
                             <tr height="35px">
                                    <td><span>Type:</span></td>
                                    <td><select name="type" >'.$this->buildType($this->oCursor['type']) . '</select></td>
                            </tr>
                            <tr height="35px">
                                    <td></td>
                                    <td><input type="submit" value="Submit"/></td>
                            </tr>
                            ';

            $this->Html .= '</form></table></div></div>';
        } else {
            $this->Html = '<div class="ctl-ct badgeswrap"><h3>Edit Badges</h3>';
            $this->Html .= '<div style="padding:50px;">';
            $this->Html .= '<span>Edit sucessful!</span><span><a href="/managebadges">Back to manage badges.</a></span>';
            $this->Html .= '</div></div>';
        }
        return $this;
    }

    protected function setUsers() {

        $this->aPageVars['body'] .= $this->Html;

        return $this;
    }

}
